package com.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.store.configure.Result;
import com.store.entity.Area;
import com.store.mapper.AreaMapper;
import com.store.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 行政区域字典表 服务实现类
 * </p>
 *
 * @author
 * @since 2025-01-06
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Resource
    private AreaMapper areaMapper;

    @Override
    public Result<Area> queryAreaList(Area area) {
        Result<Area> result = new Result<>();

        try {
            QueryWrapper<Area> wrapper = new QueryWrapper<>();
            if (StringUtils.hasText(area.getParentCode())) {
                wrapper.lambda().eq(Area::getParentCode, area.getParentCode());
            }
            if (StringUtils.hasText(area.getName())) {
                wrapper.lambda().like(Area::getName, area.getName());
            }
            if (StringUtils.hasText(area.getLevel())) {
                wrapper.lambda().like(Area::getLevel, area.getLevel());
            }
            List<Area> areas = areaMapper.selectList(wrapper);
            result.setReturnList(areas);
            result.setMessage("success");
        } catch (Exception e) {
            result.setMessage(e.getMessage());
        }

        return result;
    }
}
